package com.ym.service.impl;

import com.ym.dao.AccountDao;
import com.ym.pojo.Account;
import com.ym.service.AccountService;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

public class AccountServiceImpl implements AccountService {

    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

//    private TransactionTemplate transactionTemplate;
//
//    public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
//        this.transactionTemplate = transactionTemplate;
//    }


    @Override
    @Transactional
    public void transfer(String soureName, String targetName, double money) {
        Account accountByName = accountDao.findAccountByName(soureName);
        Account account = accountDao.findAccountByName(targetName);
        accountByName.setMoney(accountByName.getMoney()-money);
        account.setMoney(account.getMoney()+money);
        accountDao.updateAccount(accountByName);
        //模拟异常
        //int i =1/0;
        accountDao.updateAccount(account);
    }
}
